import 'package:flutter/material.dart';
import '../demo/ui_demo.dart';
import '../demo/data_source/post.dart';
import '../wonders/framework.dart';

class MyDemo extends StatelessWidget implements Demo {
  MyDemo(this.title);
  @override
  String title;
  @override
  Widget build(BuildContext context) {
    return new HomePage(title: this.title,);
  }
}

class HomePage extends StatelessWidget {
  String title;
  HomePage({this.title});

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            title: Text(this.title),
            bottom: TabBar(
              tabs: <Widget>[
                Tab(text: "DataTable",),
                Tab(text: "PaginatedDataTable",)
              ],
            ),
            actions: <Widget>[
              IconSource("DataTable","12"),
            ],
          ),
          body: TabBarView(children: <Widget>[
            _DataTableDemo(),
            PaginateTableDemo()
          ]),
        )
    );
  }
}

class _DataTableDemo extends StatefulWidget {
  @override
  __DataTableDemoState createState() => __DataTableDemoState();
}

class __DataTableDemoState extends State<_DataTableDemo> {
  int _sortColumnIndex=0;
  bool _sortAscending = true;
  @override
  Widget build(BuildContext context) {
    return Container(
        child: ListView(
      children: <Widget>[
        TableLayout.table(
          key: GlobalKey(),
            sortColumnIndex: _sortColumnIndex,
            sortAscending: _sortAscending,
            columns: [
              DataColumn(
                  label: Container(
                    child: Text("Title"),
                    width: 80.0,
                  ),
                  onSort: (int columnIndex, bool sortAscending) {
                    _sortColumnIndex=columnIndex;
                    _sortAscending=sortAscending;
                    setState(() {
                      posts.sort((a,b){
                        if (!sortAscending) {
                          final c = a;
                          a = b;
                          b = c;
                        }
                        //return a.id.compareTo(b.id);
                        return a.title.compareTo(b.title);
                      });
                    });
                  }),
              DataColumn(label: Text("Autor"),
                  onSort: null),
              DataColumn(label: Text("Image")),
            ],
            rows: posts.map((Post post) {
              return DataRow(
                  selected: post.selected,
                  onSelectChanged: (bool isSelected){
                    setState(() {
                      if(isSelected!=null){
                        post.selected=isSelected;
                      }
                    });
                  },
                  cells: [
                DataCell(Text(post.title)),
                DataCell(Text(post.author)),
                DataCell(Image.network(post.imageUrl)),
              ]
              );
            }).toList()),
      ],
    ));
  }
}
class PaginateTableDemo extends StatefulWidget {
  @override
  _PaginateTableDemoState createState() => _PaginateTableDemoState();
}

class DataTableSourceDemo extends DataTableSource{
  List<Post> _posts=posts;
  int _selectCount=0;
  @override
  DataRow getRow(int index) {
      return DataRow.byIndex(
        index: index,
        cells: <DataCell>[
          DataCell(Text(_posts[index].title,)),
          DataCell(Text(_posts[index].author)),
          DataCell(Image.network(_posts[index].imageUrl)),
        ]
      );
  }

  @override
  int get selectedRowCount=>_selectCount;

  @override
  bool get isRowCountApproximate =>false;

  @override
  int get rowCount {
    return _posts.length;
  }
  _sort(int index,bool ascending){
    _posts.sort((a,b){
      if (!ascending) {
        final c = a;
        a = b;
        b = c;
      }
      return a.title.compareTo(b.title);
    });
    notifyListeners();
  }
}

class _PaginateTableDemoState extends State<PaginateTableDemo> {
  final DataTableSourceDemo dataTableSource=DataTableSourceDemo();
  int _sortColumnIndex=0;
  bool _sortAscending = true;
  @override
  Widget build(BuildContext context) {
    debugPrint("build _PaginateTableDemoState");
    return Container(
      child: ListView(
        children: <Widget>[
          TableLayout.pagedTable(
              sortColumnIndex: _sortColumnIndex,
              sortAscending: _sortAscending,
              rowsPerPage: 8,
              header: Text("PaginatedDataTable"),
              columns: [
                DataColumn(
                    label: Container(
                      child: Text("Title"),
                    ),
                  onSort: (int columIndex,bool ascending){
                    dataTableSource._sort(columIndex,ascending);
                      setState(() {
                        _sortColumnIndex=columIndex;
                        _sortAscending=ascending;
                      });

                  }
                ),
                DataColumn(label: Text("Author")),
                DataColumn(label: Text("Image")),
              ],
              source: dataTableSource
          )
        ],
      ),
    );
  }
}

